package com.bjqh.edu.UI.Activity.Index;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

import com.baijia.player.playback.LivePlaybackSDK;
import com.baijiahulian.BJVideoPlayerSDK;
import com.baijiahulian.livecore.context.LPConstants;
import com.baijiahulian.livecore.context.LPError;
import com.baijiahulian.livecore.context.LiveRoom;
import com.baijiahulian.livecore.launch.LPLaunchListener;
import com.baijiahulian.livecore.ppt.LPPPTFragment;
import com.baijiahulian.player.BJPlayerView;
import com.baijiahulian.player.playerview.BJBottomViewPresenter;
import com.baijiahulian.player.playerview.BJCenterViewPresenter;
import com.baijiahulian.player.playerview.BJTopViewPresenter;
import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Bean.Getoken;
import com.bjqh.edu.R;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

public class Video2Activity extends AppCompatActivity {

    @BindView(R.id.playerView)
    BJPlayerView playerView;
    int rid;
    String token;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_video2);
        ButterKnife.bind(this);
        BJVideoPlayerSDK.getInstance().init(getApplication());
        playerView.setBottomPresenter(new BJBottomViewPresenter(playerView.getBottomView()));
        playerView.setTopPresenter(new BJTopViewPresenter(playerView.getTopView()));
        playerView.setCenterPresenter(new BJCenterViewPresenter(playerView.getCenterView()));
        playerView.initPartner(37415593, BJPlayerView.PLAYER_DEPLOY_ONLINE);
        rid = getIntent().getExtras().getInt("rid");
        OkGo.post(AppConstants.BASE_URL + "/rtu/getPayToken.shtml")
                .tag(this)
                .cacheKey("cacheKey")
                .cacheMode(CacheMode.DEFAULT)
                .params("vid", rid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.i("rid", rid+"");
                        Log.i("房间token", s);
                        Gson gson = new Gson();
                        Getoken getoken = gson.fromJson(s, Getoken.class);
                        if (getoken.getMsg().equals("success"))
                        {
                            token= getoken.getToken();
                            playerView.setVideoId(Long.valueOf(rid), token);
                            //播放
                            playerView.playVideo();
                        }
                    }
                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        Toast.makeText(Video2Activity.this,"报告小主，服务器被外星人偷走啦~~",Toast.LENGTH_SHORT).show();
                    }
                });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (playerView != null) {
            playerView.onDestroy();
        }
    }
}
